package com.xinyi.smart_translator
import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Bundle
import androidx.annotation.NonNull
import android.graphics.Color
import com.xinyi.smart_translator.audioservice.AudioStreamService2
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
import android.provider.Settings
import android.widget.Toast

class MainActivity : FlutterActivity() {
    private val CHANNEL = "com.xinyi.smart_translator/channel"

    companion object {
        lateinit var flutterEngine: FlutterEngine;
        const val OVERLAY_PERMISSION_REQUEST_CODE = 1001
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }




    override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)
        MainActivity.flutterEngine = flutterEngine;

        MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler { call, result ->
            when (call.method) {
                "startRecording" -> {
                    startService()
                    result.success("done!") // 无返回值
                }
                "stopRecording" -> {
                    stopService();
                    result.success("done!")
                }
                "checkOverlayPermission" -> {
                    checkOverlayPermission(this);
                    result.success("done!")
                }
                "startMainActivity" -> {
                    val mainActivityIntent = Intent(this, Class.forName("com.xinyi.smart_translator.MainActivity"))
                    startActivity(mainActivityIntent)
                    result.success("done!")
                }
                else -> {
                    result.notImplemented()
                }
            }
        }
    }


    private fun startService() {
        val serviceIntent = Intent(this, AudioStreamService2::class.java)
        startService(serviceIntent)
    }

    private fun stopService() {
        val serviceIntent = Intent(this, AudioStreamService2::class.java)
        stopService(serviceIntent)
    }




    fun checkOverlayPermission(activity: Activity): Boolean {
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (!Settings.canDrawOverlays(activity)) {
                val intent = Intent(
                    Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                    Uri.parse("package:${activity.packageName}")
                )
                activity.startActivityForResult(intent, OVERLAY_PERMISSION_REQUEST_CODE)
                false
            } else {
                true
            }
        } else {
            true
        }
    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == OVERLAY_PERMISSION_REQUEST_CODE) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (Settings.canDrawOverlays(this)) {
                    Toast.makeText(context,"已经授权", Toast.LENGTH_SHORT).show();
                }
            }
        }
    }


}

